/*! \file gd.cpp
 *	\brief
 *
 *	created:	2009/11/12
 *	created:	12:11:2009   16:10
 *	filename: 	c:\src\main\libs\gd\gd.cpp
 *	file path:	c:\src\main\libs\gd
 *	file base:	gd
 *	file ext:	cpp
 *	author:		Vitalii Trubchaninov
 *				venngraydog@gmail.com
 */
#include "gd/stdafx.h"

#include "gd/gd.h"
#include "utils/Library.h"

namespace tt{
namespace gd{

typedef UInt (*DriversCountFunc)();
typedef SharedPtr<Driver> (*CreateDriverFunc)(UInt driverIndex);

const char* DRIVERS_COUNT_FUNC_NAME = "DriversCount";
const char* CREATE_DRIVER_FUNC_NAME = "CreateDriver";

SharedPtr<Driver> createDriver( const SharedPtr<utils::Library>& library, UInt driverIndex /*= 0*/ )
{
	if (!library->valid())
		return SharedPtr<Driver>();

	DriversCountFunc driverCountFunc = (DriversCountFunc)library->GetProcAddress(DRIVERS_COUNT_FUNC_NAME);
	if (!driverCountFunc)
		return SharedPtr<Driver>();

	UInt count = driverCountFunc();
	if (count <= driverIndex)
		return SharedPtr<Driver>();

	CreateDriverFunc createDriverFunc = (CreateDriverFunc)library->GetProcAddress(CREATE_DRIVER_FUNC_NAME);
	if (!createDriverFunc)
		return SharedPtr<Driver>();

	SharedPtr<Driver> driver = createDriverFunc(driverIndex);

	return driver;
}

} //namespace gd
} //namespace tt
